<template>
  <div>
    <el-dialog
      class="custom-dialog"
      v-model="dialogVisible"
      title="访客审核"
      width="500px"
      @close="handleClose"
    >
      <el-form
        class="data-form"
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="是否通过" prop="applyStatus">
              <el-radio-group v-model="form.applyStatus">
                <el-radio
                  v-for="(item, index) of dict['isPass']"
                  :key="index"
                  :value="item.value"
                  >{{ item.name }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="审核意见">
              <el-input
                class="data-input"
                v-model="form.applyCause"
                placeholder="请输入"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                class="data-input"
                v-model="form.remark"
                placeholder="请输入"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="handleClose(false)" :loading="loading"
          >取消</el-button
        >
        <el-button type="primary" @click="handleSubmit" :loading="loading"
          >确定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, watch, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { cardId } from '@/utils/validate'
import { useDict } from '@/hooks/useDict'
const { dict } = useDict(['isPass'])
import { CirclePlusFilled, RemoveFilled } from '@element-plus/icons-vue'
import { visitorCheck } from '@/api/manager/visitorManage/reservationRecord.js'
defineOptions({
  name: 'AddDialog',
})
const props = defineProps({
  detail: {
    type: Object,
  },
})
const emit = defineEmits(['close'])
const title = ref('')
const dialogVisible = ref(false)
const handleClose = (isFlash) => {
  form.value = {
    applyStatus: '1',
  }
  formRef.value.clearValidate()
  dialogVisible.value = false
  emit('close', isFlash)
}
const formRef = ref(null)
const form = ref({
  applyStatus: '1',
})

const rules = ref({
  applyStatus: [{ required: true, message: '请选择是否通过', trigger: 'blur' }],
})

const loading = ref(false)
const handleSubmit = async () => {
  if (!formRef.value) return
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true
      const params = JSON.parse(JSON.stringify(form.value))
      params.resourceId = props.detail.pkid

      try {
        let res = await visitorCheck(params)
        if (res.success) {
          ElMessage.success('审核成功')
          handleClose(true)
        } else {
          ElMessage.error(res.message)
        }
        loading.value = false
      } catch (error) {
        console.log('error', error)
        loading.value = false
      }
    } else {
      ElMessage.warning('请正确录入信息')
    }
  })
}
defineExpose({ dialogVisible })
onMounted(() => {})
</script>
<style lang="scss" scoped></style>
